package com.chuangke.flow.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum FlowNodeStatusEnum {

    unstart("0", "", "未启动"),
    unhandle("1", "", "待处理"),
    nohandle("2", "", "他人已审批"),
    agree("7", "agree", "同意"),
    revoke("8", "revoke", "撤回"),
    reject("9", "reject", "退回");

    FlowNodeStatusEnum(String code, String command, String desc) {
        this.code = code;
        this.command = command;
        this.desc = desc;
    }

    private String code;
    private String command;//前台审批传入的指令
    private String desc;

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public static Map<String, Map<String, Object>> toMap() {
        FlowNodeStatusEnum[] ary = FlowNodeStatusEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<>();
        for (FlowNodeStatusEnum flowNodeStatusEnum : ary) {
            Map<String, Object> map = new HashMap<>();
            String key = flowNodeStatusEnum.getCode();
            map.put("code", flowNodeStatusEnum.getCode());
            map.put("desc", flowNodeStatusEnum.getDesc());
            enumMap.put(key, map);
        }
        return enumMap;
    }

    public static List<Map<String, String>> toList() {
        FlowNodeStatusEnum[] ary = FlowNodeStatusEnum.values();
        List<Map<String, String>> list = new ArrayList<>();
        for (FlowNodeStatusEnum flowNodeStatusEnum : ary) {
            Map<String, String> map = new HashMap<>();
            map.put("code", flowNodeStatusEnum.getCode());
            map.put("desc", flowNodeStatusEnum.getDesc());
            list.add(map);
        }
        return list;
    }

    public static FlowNodeStatusEnum getEnum(String code) {
        FlowNodeStatusEnum[] arry = FlowNodeStatusEnum.values();
        for (FlowNodeStatusEnum flowNodeStatusEnum : arry) {
            if (flowNodeStatusEnum.getCode().equalsIgnoreCase(code)) {
                return flowNodeStatusEnum;
            }
        }
        return null;
    }

    /**
     * 取枚举的json字符串
     *
     * @return
     */
    public static String getJsonStr() {
        FlowNodeStatusEnum[] enums = FlowNodeStatusEnum.values();
        StringBuilder jsonStr = new StringBuilder("[");
        for (FlowNodeStatusEnum senum : enums) {
            if (!"[".equals(jsonStr.toString())) {
                jsonStr.append(",");
            }
            jsonStr.append("{code:'").append(senum.getCode()).append("',desc:'").append(senum.getDesc()).append("'}");
        }
        jsonStr.append("]");
        return jsonStr.toString();
    }

}
